﻿using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1.Test
{
    public partial class Form2 : Form
    {
        private FastObjectListView folv;
        public Form2()
        {
            InitializeComponent();
        }
        private void InitializeComponents()
        {
            // 初始化FastObjectListView
            folv = new FastObjectListView();
            folv.Dock = DockStyle.Fill;
            folv.View = View.Details;
            folv.ShowGroups = true; // 启用分组
            folv.GroupWithItemCount = true; // 显示每组的项数量

            // 添加列
            folv.AllColumns.Add(new OLVColumn("ID", "Id") { Width = 60 });
            folv.AllColumns.Add(new OLVColumn("名称", "Name") { Width = 150 });
            folv.AllColumns.Add(new OLVColumn("类别", "Category") { Width = 100 });
            folv.AllColumns.Add(new OLVColumn("价格", "Price") { Width = 80 });

            this.Controls.Add(folv);
            this.Size = new Size(600, 400);
        }

        private void LoadData()
        {
            // 加载示例数据
            var data = new List<Product>
        {
            new Product { Id = 1, Name = "苹果", Category = "水果", Price = 5.99m },
            new Product { Id = 2, Name = "香蕉", Category = "水果", Price = 3.99m },
            new Product { Id = 3, Name = "白菜", Category = "蔬菜", Price = 2.50m },
            new Product { Id = 4, Name = "牛肉", Category = "肉类", Price = 35.99m },
            new Product { Id = 5, Name = "猪肉", Category = "肉类", Price = 28.50m },
            new Product { Id = 6, Name = "橙子", Category = "水果", Price = 4.50m },
            new Product { Id = 7, Name = "萝卜", Category = "蔬菜", Price = 1.99m }
        };

            folv.SetObjects(data);
        }

        private void SetupGrouping()
        {
            // 1. 设置分组规则：按Category字段分组
            folv.GroupKeyGetter = (model) =>
            {
                var product = model as Product;
                return product?.Category; // 分组键
            };

            // 2. 自定义组标题（默认显示"分组键 (项数)"）
            folv.GroupFormatter = (group, header) =>
            {
                // 组标题格式："类别: 水果 (3项)"
                header.Text = $"类别: {group.Key} ({group.ItemCount}项)";
            };

            // 3. 自定义组外观
            folv.GroupHeaderFont = new Font(folv.Font, FontStyle.Bold);
            folv.GroupHeaderForeColor = Color.DarkBlue;
            folv.GroupWithItemCount = true; // 显示项数量
            folv.ExpandAllGroups(); // 初始展开所有组
        }
    }

    // 数据模型类
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }
}
